package com.ctlele.team.space.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ctlele.team.space.R;
import com.ctlele.team.space.activity.BaseActivity;
import com.ctlele.team.space.activity.impl.ChatWithActivity;
import com.ctlele.team.space.entity.vo.FriendItemVo;
import com.ctlele.team.space.util.ImageUtils;
import com.ctlele.team.space.util.UserContext;
import com.ctlele.team.space.widget.dialog.MsgDialog;
import com.ctlele.team.space.widget.toast.IToast;
import com.squareup.picasso.Picasso;

import java.util.List;

public class FriendItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final String TAG = "FriendItemAdapter";

    private final BaseActivity mActivity;

    private List<FriendItemVo> mFriendList;

    public FriendItemAdapter(BaseActivity activity) {
        mActivity = activity;
    }

    public void flushData(List<FriendItemVo> fileList) {
        mFriendList = fileList;
        mActivity.runOnUiThread(() -> {
            notifyDataSetChanged();
        });
        IToast.success(mActivity, "数据刷新成功");
    }

    public void appendData(List<FriendItemVo> friendList) {
        if (friendList == null || friendList.isEmpty()) {
            IToast.alert(mActivity, "没有更多数据");
            return;
        }
        if (mFriendList == null || mFriendList.isEmpty()) {
            flushData(friendList);
            return;
        }

        int size = mFriendList.size();
        mFriendList.addAll(friendList);
        mActivity.runOnUiThread(() -> {
            notifyItemRangeChanged(size, mFriendList.size());
        });
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mActivity).inflate(R.layout.item_friend_layout, parent, false);
        return new FriendItemAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder vh = (ViewHolder) holder;
        FriendItemVo vo = mFriendList.get(position);

        vh.nickname.setText(vo.getNickname());
        vh.username.setText(vo.getUsername());
        Picasso.with(mActivity).load(vo.getAvatar()).transform(ImageUtils.getCircleImageTransformation()).into(vh.avatar);

        vh.rootView.setOnClickListener(v -> {
            mActivity.runOnUiThread(() -> {
                Intent chatWithView = new Intent(mActivity, ChatWithActivity.class);
                chatWithView.putExtra(ChatWithActivity.ME_KEY, UserContext.get().getUsername());
                chatWithView.putExtra(ChatWithActivity.TO_KEY, vo.getUsername());
                chatWithView.putExtra(ChatWithActivity.TO_AVATAR_KEY, vo.getAvatar());
                chatWithView.putExtra(ChatWithActivity.ME_AVATAR_KEY, UserContext.get().getAvatar());
                mActivity.startActivity(chatWithView);
            });
        });
    }

    @Override
    public int getItemCount() {
        return mFriendList == null ? 0 : mFriendList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView nickname;

        private final TextView username;

        private final ImageView avatar;

        private final View rootView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            nickname = itemView.findViewById(R.id.nickname);
            username = itemView.findViewById(R.id.username);
            avatar = itemView.findViewById(R.id.friend_avatar);
            rootView = itemView.findViewById(R.id.root_view);

            itemView.setTag(this);
        }
    }
}
